package com.nodecollege.cloud.controller.admin;

import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.BindVO;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsMenu;
import com.nodecollege.cloud.common.model.po.UpmsRole;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.service.RoleMenuService;
import com.nodecollege.cloud.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 应用预制角色管理
 *
 * @author LC
 * @date 2020/1/14 13:38
 */
@RestController
@RequestMapping("/appRole")
public class AppRoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleMenuService roleMenuService;

    @ApiAnnotation(modularName = "应用角色管理", description = "查询应用预制角色列表")
    @PostMapping("/getRoleListByAppId")
    public NCResult<UpmsRole> getRoleListByAppId(@RequestBody QueryVO<UpmsRole> queryVO) {
        queryVO.getData().setRoleSource(NCConstants.INT_0);
        return roleService.getRoleList(queryVO);
    }

    @ApiAnnotation(modularName = "应用角色管理", description = "添加应用预制角色")
    @PostMapping("/addRoleByAppId")
    public NCResult addRoleByAppId(@RequestBody UpmsRole role) {
        role.setRoleSource(NCConstants.INT_0);
        roleService.addRole(role);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "应用角色管理", description = "更新应用预制角色")
    @PostMapping("/updateRoleByAppId")
    public NCResult updateRoleByAppId(@RequestBody UpmsRole role) {
        role.setRoleSource(NCConstants.INT_0);
        roleService.updateRole(role);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "应用角色管理", description = "删除应用预制角色")
    @PostMapping("/delRoleByAppId")
    public NCResult delRoleByAppId(@RequestBody UpmsRole role) {
        role.setRoleSource(NCConstants.INT_0);
        roleService.delRole(role);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "应用角色管理", description = "锁定解锁应用预制角色")
    @PostMapping("/lockRoleByAppId")
    public NCResult lockRoleByAppId(@RequestBody UpmsRole role) {
        role.setRoleSource(NCConstants.INT_0);
        roleService.lockRole(role);
        return NCResult.ok();
    }

    @ApiAnnotation(modularName = "角色管理", description = "查询角色拥有菜单列表")
    @PostMapping("/getMenuListByRoleId")
    public NCResult<UpmsMenu> getMenuListByRoleId(@RequestBody UpmsRole role) {
        role.setRoleSource(NCConstants.INT_0);
        return NCResult.ok(roleMenuService.getMenuListByRoleId(role));
    }

    @ApiAnnotation(modularName = "角色管理", description = "绑定/解绑角色菜单关系")
    @PostMapping("/bindRoleMenuByRoleId")
    public NCResult bindRoleMenuByRoleId(@RequestBody BindVO bindVO) {
        NCUtils.nullOrEmptyThrow(bindVO.getAppId(), new UpmsException("", "应用id必填！"));
        bindVO.setTenantId(null);
        bindVO.setMainSource(true);
        roleMenuService.bindRoleMenu(bindVO);
        return NCResult.ok();
    }
}
